package xiangMu.pc;

public class Pc {
    private String series;  //型号(包括品牌和系列)
    private int price;  //单位为元
    private String resolutionRatio;  //分辨率
    private int diskCapacity;   //硬盘容量(兆作为单位)
    private Disk[] disks = new Disk[6];    //盘符
    private int countDisk = 0;
    private OS os;  //操作系统
    private boolean state = false;  //电脑状态(启动和关机)。默认状态为关机
    private Software[] softwares = new Software[30];  //电脑上的软件(普通软件和下载软件，不包括操作系统和硬盘盘符)
    private int countSoftwares = 0;


    public Pc() {
    }


    //启动电脑
    public void start() {
        state = true;
        System.out.println("启动成功");
    }


    //启动软件
    public void startSoftware(Software software) {
        System.out.println(software.getName() + "启动!");
    }

    //格式化
    public boolean setDisks(Disk[] disks) {
        int sum = 0;
        for (Disk a : disks)
            sum += a.getSize();
        if (sum > diskCapacity)
            return false;
        this.disks = disks;
        countDisk = disks.length;
        return true;
    }


    //安装操作系统
    public boolean installOS(OS os, Disk disk) {
        if (os.getSize() > disk.getSize())
            return false;
        this.os = os;
        disk.setSize(disk.getSize() - os.getSize());
        disk.addInstallSoftware(os);//操作系统直接安装在Pc某个盘符上，不需要下载
        softwares[0] = new DownloadSoftware("应用商城", 300);    //初始化一个Pc操作系统自带的，具有下载功能的软件，容量大小包括在操作系统中
        softwares[0].setState(true);//已安装状态
        countSoftwares = 1;
        return true;
    }


    //下载软件(借助具有下载功能的软件实现下载功能)
    public boolean downloadSoft(DownloadSoftware downloadSoftware, Software software, Disk disk) {
        if (software.getSize() > disk.getSize())
            return false;

        addSoftware(downloadSoftware.downloadSoftware(software));//调用下载软件，实现下载软件的功能，同时把下载的软件添加到Pc的软件数组softwares中
        disk.setSize(disk.getSize() - software.getSize());
        disk.addDownloadSoftware(software);//添加到盘符的已下载软件数组中
        return true;
    }


    //安装软件(Pc有安装功能)
    public boolean installSoftware(Software software, Disk disk) {
        if (software.isState()) {   //下载的软件是已安装状态，不能重复安装
            System.out.println("软件已安装，不能重复安装");
            return false;
        }
        Software software1 = new Software(software.getName(), software.getSize() * 3); //Pc安装软件，得到一个新的可执行exe软件，安装软件占用的容量是下载软件的3倍
        if (software1.getSize() > disk.getSize())
            return false;
        software.setState(true);//下载的软件是已安装状态
        disk.setSize(disk.getSize() - software1.getSize());
        disk.addInstallSoftware(software1);//添加到盘符的已安装软件数组中
        return true;
    }


    //电脑添加软件(包括普通软件和下载软件)
    public void addSoftware(Software software) {
        expandSoftwares();
        softwares[countSoftwares++] = software;
    }


    //辅助方法，安装软件时，根据数字选择显示软件，且为未安装状态，且有布尔型返回值   1:普通软件/2:下载软件
    public boolean showSoftware1(int n) {
        if (countSoftwares == 1) {
            System.out.println("无已下载的软件");
            return false;
        }
        if (n == 1) {
            int hasSoftware1 = 0;
            for (int i = 0; i < countSoftwares; i++) {
                if (!(softwares[i] instanceof DownloadSoftware) && !softwares[i].isState()) {
                    System.out.print(softwares[i].getName() + " ");
                    hasSoftware1++;
                }
            }
            if (hasSoftware1 == 0) {
                System.out.println("无可安装的普通软件");
                return false;
            }
            System.out.println();
        } else if (n == 2) {
            int hasSoftware2 = 0;
            for (int i = 0; i < countSoftwares; i++) {
                if (softwares[i] instanceof DownloadSoftware && !softwares[i].isState()) {
                    System.out.print(softwares[i].getName() + " ");
                    hasSoftware2++;
                }
            }
            if (hasSoftware2 == 0) {
                System.out.println("无可安装的具有下载功能的软件");
                return false;
            }
            System.out.println();
        }
        return true;
    }


    //辅助方法，下载软件时，显示具有下载功能的软件，且为安装状态
    public void showSoftware2() {
        for (int i = 0; i < countSoftwares; i++) {
            if (softwares[i] instanceof DownloadSoftware && softwares[i].isState())
                System.out.print(softwares[i].getName() + " ");
        }
        System.out.println();
    }


    //辅助方法，寻找名字对应的盘符
    public Disk searchDisk(String name) {
        for (int i = 0; i < countDisk; i++) {
            if (disks[i].getName().equals(name))
                return disks[i];
        }
        return null;
    }


    //辅助方法，寻找名字对应的软件
    public Software searchSoftware(String name) {
        for (int i = 0; i < countSoftwares; i++) {
            if (softwares[i].getName().equals(name))
                return softwares[i];
        }
        return null;
    }


    //辅助方法，判断Pc是否安装了操作系统
    public boolean judgeHasOS() {
        if (os == null)
            return false;
        else
            return true;
    }


    //辅助方法，判断Pc是否已格式化
    public boolean judgeHasFormat() {
        if (countDisk == 0)
            return false;
        else
            return true;
    }


    //给Pc下载的软件数组扩容
    public void expandSoftwares() {
        if (countSoftwares >= softwares.length) {
            Software[] softwares1 = new Software[softwares.length * 2];
            for (int i = 0; i < countSoftwares; i++) {
                softwares1[i] = softwares[i];
            }
            softwares = softwares1;
        }
    }

    public String getSeries() {
        return series;
    }

    public void setSeries(String series) {
        this.series = series;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getResolutionRatio() {
        return resolutionRatio;
    }

    public void setResolutionRatio(String resolutionRatio) {
        this.resolutionRatio = resolutionRatio;
    }

    public int getDiskCapacity() {
        return diskCapacity;
    }

    public void setDiskCapacity(int diskCapacity) {
        this.diskCapacity = diskCapacity;
    }

    public Disk[] getDisks() {
        return disks;
    }

    public boolean isState() {
        return state;
    }

    public void setState(boolean state) {
        this.state = state;
    }

    public int getCountSoftwares() {
        return countSoftwares;
    }
}
